/**
 * Created by JetBrains PhpStorm.
 * User: maplefalling
 * Date: 2/12/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */

/*
 data:hard coded property
 extend
 requires:array
 config:object
 constructor:
 */

Ext.define = function (className, data, callBack) {
    var superclass = Object;
    var config = {};
    if (data) {

        //set up superclass
        if (data.extend && Ext.isString(data.extend)) {
            var extendClassPath = Ext.util.Format.trim(data.extend);
            var ns = extendClassPath.split('.');
            superclass = window[ns[0]];
            var current = window[ns[0]];
            var sub = ns.slice(1);
            var len2 = sub.length;
            for (j = 0; j < len2; ++j) {
                if (superclass.hasOwnProperty(sub[j])) {
                    superclass = superclass[sub[j]];
                } else {
                    throw "invalid super class path";
                }
            }

            if (!superclass)
                throw "invalid super class";
        }
        extendClassPath = null;
        delete data.extend;
        config = Ext.apply(config, {superclass:superclass});

    }
    /*We won't support callParent, because it is handled by Sencha 2 inside framework,Sencha 1 does not have this feature
     var callParentFn=function(){
     var method=arguments.callee.caller.name;//won't work on IE
     if (this.superclass){
     var superMethod=this.superclass['"'+method+'"'];
     superMethod.apply(this,arguments);
     }

     }

     config=Ext.apply(config,{callParent:callParentFn});*/
    //set up config
    if (data.config) {
        Ext.apply(config, data.config);
        delete data.config;
    }
    Ext.apply(config, data);
    className = Ext.util.Format.trim(className);
    var ns = className.split('.');
    var current = window[ns[0]];
    var sub = ns.slice(1);
    var len2 = sub.length - 1;
    for (j = 0; j < len2; ++j) {
        current = current[sub[j]] = current[sub[j]] || {};
    }
//    console.log(sub[len2]);
    current[sub[len2]] = Ext.extend(superclass, config);


};


Ext.create2 = function (classname, config) {
    if (classname && Ext.isString(classname)) {
        classname = Ext.util.Format.trim(classname);
        var _classDef = null;
        var _classPaths = classname.extend.split('.');
        var ns = className.split('.');
        var current = window[ns[0]];
        var sub = ns.slice(1);
        var len2 = sub.length - 1;
        for (j = 0; j < len2; ++j) {
            current = current[sub[j]] = current[sub[j]] || {};
        }
        var _instance = new current(config);
        return _instance;

    } else {
        throw "invalid classname define in Ext.create2"
    }
};

String.prototype.capitalize = function (string) {
    return string.charAt(0).toUpperCase() + string.substr(1);
};

Array.prototype.from = function (value, newReference) {
    if (value === undefined || value === null) {
        return [];
    }

    if (Ext.isArray(value)) {
        return (newReference) ? slice.call(value) : value;
    }

    if (value && value.length !== undefined && typeof value !== 'string') {
        return Ext.toArray(value);
    }

    return [value];
};

Function.prototype.pass = function (fn, args, scope) {
    if (!Ext.isArray(args)) {
        args = Ext.Array.clone(args);
    }

    return function () {
        args.push.apply(args, arguments);
        return fn.apply(scope || this, args);
    };
};
/*
 Ext.override(Ext.data.WebStorageProxy,{
 //override the data here to call rconvert of the fields
 setRecord: function(record, id) {
 if (id) {
 record.setId(id);
 } else {
 id = record.getId();
 }

 var rawData = record.data,
 data    = {},
 model   = this.model,
 fields  = model.prototype.fields.items,
 length  = fields.length,
 i, field, name;

 for (i = 0; i < length; i++) {
 field = fields[i];
 name  = field.name;

 if (typeof field.encode == 'function') {
 data[name] = field.encode(rawData[name], record);
 } else {
 data[name] = rawData[name];
 }
 }

 var obj = this.getStorageObject(),
 key = this.getRecordKey(id);

 //keep the cache up to date
 this.cache[id] = record;

 //iPad bug requires that we remove the item before setting it
 obj.removeItem(key);
 obj.setItem(key, Ext.encode(data));
 }
 })*/
